package com.company;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * https://juejin.cn/post/7139741080597037063
 * @date : 2023-09-01 下午1:39
 */

public class P064_SafePoint {

    public static class Main01 {

        public static AtomicInteger NUM = new AtomicInteger(0);

        public static void main(String[] args) throws InterruptedException {
            Runnable runnable = () -> {
                for (int i = 0; i < 10_0000_0000; i++) {
                    NUM.getAndAdd(1);
                }
                System.out.println(Thread.currentThread().getName() + "执行结束" + NUM);
            };
            Thread t1 = new Thread(runnable);
            Thread t2 = new Thread(runnable);
            t1.start();
            t2.start();
            Thread.sleep(1000);
            System.out.println("NUM = " + NUM);
        }
    }

    public static class Main02 {
        public static AtomicInteger NUM = new AtomicInteger(0);

        public static void main(String[] args) throws InterruptedException {
            Runnable runnable = () -> {
                for (long i = 0; i < 10_0000_0000; i++) {
                    NUM.getAndAdd(1);
                }
                System.out.println(Thread.currentThread().getName() + "执行结束" + NUM);
            };
            Thread t1 = new Thread(runnable);
            Thread t2 = new Thread(runnable);
            t1.start();
            t2.start();
            Thread.sleep(1000);
            System.out.println("NUM = " + NUM);
        }
    }
}
